// PalView.cpp : implementation file
//

#include "stdafx.h"
#include "U7FileTool.h"

#include "PalFrame.h"
#include "PalDoc.h"
#include "PalView.h"


// CPalView

IMPLEMENT_DYNCREATE(CPalView, CScrollView)

CPalView::CPalView()
{

}

CPalView::~CPalView()
{
}


BEGIN_MESSAGE_MAP(CPalView, CScrollView)
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()


// CPalView drawing

void CPalView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	// TODO: calculate the total size of this view
	sizeTotal.cx = sizeTotal.cy = 256;
	SetScrollSizes(MM_TEXT, sizeTotal);

	CPalDoc* pDoc = (CPalDoc*)GetDocument();
	int count = pDoc->GetPaletteCount();

	CPalFrame* pFrame = (CPalFrame*)GetParent();
	pFrame->UpdatePalInfo( 0, count );
}

void CPalView::OnDraw(CDC* pDC)
{
	CPalDoc* pDoc = (CPalDoc*)GetDocument();
	// TODO: add draw code here
	if ( !pDoc || !pDoc->GetPaletteCount() )
		return;

	CPalFrame* pFrame = (CPalFrame*)GetParent();
	int palIndex = pFrame->GetCurrentPalIndex();

	PCOLORENTRY pPalFile = pDoc->GetPaletteFile(palIndex);

	CSize sz = GetTotalSize();

	int nBlockW = sz.cx / 16;
	int nBlockH = sz.cy / 16;

	if ( pPalFile )
	{
		int colorIndex = 0;
		for ( int row = 0, y = 0; row < 16; row++, y+=nBlockH )
		{
			for ( int col = 0, x = 0; col < 16; col++, x+=nBlockW )
			{
				PCOLORENTRY pColor = &pPalFile[colorIndex];
				pDC->FillSolidRect( x, y, nBlockW, nBlockH,
					RGB(pColor->red<<2,pColor->green<<2,pColor->blue<<2) );
				colorIndex++;
			}
		}
	}
	else
	{
		pDC->PatBlt( 0, 0, sz.cx, sz.cy, BLACKNESS );
	}
}


// CPalView diagnostics

#ifdef _DEBUG
void CPalView::AssertValid() const
{
	CScrollView::AssertValid();
}

#ifndef _WIN32_WCE
void CPalView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
#endif
#endif //_DEBUG


// CPalView message handlers


BOOL CPalView::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	//return CScrollView::OnEraseBkgnd(pDC);

	CRect rc;
	GetClientRect(rc);

	CSize sz = GetTotalSize();

	if ( rc.Width() > sz.cx )
	{
		pDC->FillSolidRect( sz.cx, 0, rc.Width() - sz.cx, sz.cy, RGB(200,200,200) );
	}

	if ( rc.Height() > sz.cy )
	{
		pDC->FillSolidRect( 0, sz.cy, rc.Width(), rc.Height() - sz.cy, RGB(200,200,200) );
	}

	return TRUE;
}
